package com.jni.riyada;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.apache.http.NameValuePair;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.jni.adapters.NewsAdapter;
import com.jni.model.Lnews;
import com.jni.parsers.LastNewsParser;
import com.jni.ws.JSONParser;

public class LayoutThree extends Fragment {

	TextView article_text;

	public static Fragment newInstance(Context context) {
		LayoutThree f = new LayoutThree();

		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		new LoadLNews().execute("");
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_three,
				null);
		((ImageView) root.findViewById(R.id.imageLnews))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						extendLnews(v);
					}
				});
		return root;
	}

	/*
	 * /AsyncTask
	 */
	class LoadLNews extends AsyncTask<String, Void, ArrayList<Lnews>> {
		// Context

		public LoadLNews() {

		}

		protected ArrayList<Lnews> doInBackground(String... args) {
			LastNewsParser newsParser = null;
			Log.i("LayoutThree", "ok");
			JSONParser parser = new JSONParser();
			Log.i("LayoutThree", "parser");
			List<NameValuePair> params = null;
			URL url;
			try {
				Log.i("LayoutThree", "inside try");
				url = new URL("http://riyada.ma/Json_lastnews.php");
				URI uri;
				uri = new URI(url.getProtocol(), url.getHost(), url.getPath(),
						url.getQuery(), null);
				Log.i("LayoutThree", uri.toString());
				String jsonResult = parser.makeHttpRequest(uri.toString(),
						"GET", params);
				Log.i("LayoutThree", jsonResult);
				newsParser = new LastNewsParser(jsonResult);
				Log.i("ok", "inside diInBackground");

				/*
				 * for(Lnews news : newsParser.wsRespLNews.Lnews){
				 * Log.i("ok",""+news.article.title);
				 * article_text.setText(news.article.title); }
				 */

			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return (ArrayList<Lnews>) newsParser.wsRespLNews.Lnews;
		}

		// publishProgress(json);

		@Override
		protected void onProgressUpdate(Void... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(ArrayList<Lnews> array) {
			Log.e("ok", "inside pos execute");
			// EntryAdapter adapter = new EntryAdapter(LarynxSpeaker.this,
			// items);

			// setListAdapter(adapter);
			NewsAdapter adapter = new NewsAdapter(getActivity(), array);

			ListView list = (ListView) getActivity().findViewById(R.id.list);
			list.setAdapter(adapter);

		}

	}

	public void extendLnews(View v) {
		if (((ListView) getView().findViewById(R.id.list)).getVisibility() == View.GONE)
			((ListView) getView().findViewById(R.id.list))
					.setVisibility(View.VISIBLE);
		else
			((ListView) getView().findViewById(R.id.list))
					.setVisibility(View.GONE);
	}
}
